// index.js

// 获取应用实例
const app = getApp()

Page({
  data: {
    motto: '游戏模式选择',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: true,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    const sysInfo = wx.getSystemInfoSync();
    if(sysInfo.platform!='devtools'){
      wx.startWifi({
        success:()=>{
          this.data.isEnabledWifi=true;
          wx.getConnectedWifi({
            success:(res)=>{
              console.log('connect wifi info',res)
            },
            fail(err){
              // app.showModal(err.errMsg);
              console.error(err)
            }
          })
        },
        fail(err){
          //app.showModal(err.errMsg)
          console.error(err)
        }
      });
    }
  },
  onUnload(){
    if(this.data.isEnabledWifi){
      wx.stopWifi();
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        // const { nickName, avatarUrl } = res.userInfo;
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onClick(e){
    const { key } = e.currentTarget.dataset;
    const { avatarUrl, nickName } = this.data.userInfo;
    switch(key){
      case 'join-game':
        wx.navigateTo({
          url: '/pages/scan/scan',
          success(res){
            res.eventChannel.emit('args', {
              avatarUrl,
              nickName,
            });
          }
        });
        break;
      case 'create-game':
        wx.navigateTo({
          url: '/pages/game/game',
          success(res){
            res.eventChannel.emit('args',{
              localInfo:{
                avatarUrl,
                nickName,
              },
              IsOwner:true,
              IsRobot:false,
            })
          }
        });
        break;
      case 'robot-game':
        wx.navigateTo({
          url: '/pages/gameChoose/gameChoose',
          success(res){
            res.eventChannel.emit('args',{
              localInfo:{
                avatarUrl,
                nickName,
              },
              IsOwner:true,
              IsRobot:true,
            })
            console.log("关卡选择");
          }
        });
        break;
    }
  },
  
Click: function (e)
{
  
    // console.log(e.target.id);
    var id = e.target.id;
    if (id == "btn1") {
      let gameVs = this.selectComponent('#game_vs');
      gameVs.resetGame();
    } else if (id == "btn2")
    {
      app.showModal({
        title: '签到',
        content: '签到成功！您将获得100金币！',
        showCancel: false
    });
    var that = this;
    wx.getStorage({
      key:'coins',
      success:function(res)
      {
        var coins=res.data+100;
        that.setData({
          coins:coins
        });
        wx.setStorage({
          key: 'coins',
          data: coins // 将更新后的coins值重新写入缓存
        });
        console.log('该用户为老用户，金币为：', coins);
      },
      fail: function() {
        wx.setStorage({
          key:'coins',
          data:100
        })
        console.log('新创建用户，金币为：', coins);
      }
    });
  }
  }
})


